﻿using Axis.BusinessObject.Core;
using Axis.Dao.News;
using Axis.Web.Administrator.Global;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Xml;

namespace Axis.Web.Administrator.Views.Static.Service
{
    /// <summary>
    /// Summary description for StaticAction
    /// </summary>
    public class StaticAction : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string output = UpdateNews(context);
            context.Response.Write(output);
        }
        protected string UpdateNews(HttpContext context)
        {

            string inputValue = GetInputValue(context);
            /* Action */
            CNewsDao core = new CNewsDao();
            CApplicationMessage message = core.ActionNews(inputValue);
            string output = new JavaScriptSerializer().Serialize(message);
            return output;
        }
        public string GetInputValue(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;

            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("Root");
            XmlElement InputValue = doc.CreateElement("InputValue");
            root.AppendChild(InputValue);
            InputValue.SetAttribute("UserID", CSecurity.USER_ID.ToString());
            InputValue.SetAttribute("SecrectToken", CSecurity.SECRECT_TOKEN.ToString());

            StringBuilder qsXml = new StringBuilder();
            string[] allKeys = request.Form.AllKeys;
            if (allKeys.Length == 0)
            {
                allKeys = request.QueryString.AllKeys;
            }
            foreach (string pKey in allKeys)
            {
                string key = pKey.Trim();
                string value = request[key].Trim();

                if (key.IndexOf("Gift_") != -1)
                {
                    XmlElement gift = doc.CreateElement("Gift");
                    string giftID = key.Replace("Gift_", "");
                    gift.SetAttribute("GiftID", giftID);
                    gift.SetAttribute("Quantity", value);
                    root.AppendChild(gift);
                }
                else
                {
                    InputValue.SetAttribute(key, value);
                }
            }
            return root.InnerXml;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}